package com.zrrd.jt_admin.manager.controller;


import com.zrrd.jt_admin.common.dto.SetRulesDto;
import com.zrrd.jt_admin.common.entity.Role;
import com.zrrd.jt_admin.common.entity.RoleRule;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.page.RolePageVo;
import com.zrrd.jt_admin.manager.service.RoleRuleService;
import com.zrrd.jt_admin.manager.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-06-25
 */
@Api(tags = "角色管理")
@ApiResponses({
        @ApiResponse(code = 500, message = "服务器内部错误，请联系管理人员！"),
        @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@RestController
@RequestMapping("/admin/role")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private RoleRuleService roleRuleService;

    @ApiOperation(value = "配置权限",notes = "给角色配置权限")
    @PostMapping("/setRules")
    @ResponseBody
    //@Transactional(rollbackFor = Exception.class)
    public ResponseResult setRules(@RequestBody SetRulesDto setRulesDto) {
        if (setRulesDto == null) {
            return ResponseResult.updateResponse(false);
        }
        /*
            {
                "id":"id",
                "ruleIds":[1,3,4,5,7,9,6]
            }
        */
        log.info("获取id---->"+setRulesDto.getId());
        log.debug("获取集合---->"+setRulesDto.getRuleIds());
        Boolean flag=false;
        //获取角色id
        Integer roleId=setRulesDto.getId();
        //遍历菜单id集合
        for (Integer ruleId : setRulesDto.getRuleIds()) {
            //先根据条件roleId和ruleId作为条件查询，为空，则添加，否则跳过
            RoleRule rr = roleRuleService.lambdaQuery()
                    .eq(RoleRule::getRoleId, roleId)
                    .eq(RoleRule::getRuleId, ruleId)
                    .one();
            if (rr != null) {
                continue;
            }
            RoleRule roleRule = new RoleRule();
            roleRule.setRoleId(roleId);
            roleRule.setRuleId(ruleId);
            flag=roleRuleService.save(roleRule);
        }
        return ResponseResult.updateResponse(flag);
    }



    @ApiOperation(value = "添加", notes = "添加角色信息")
    @PostMapping
    @ResponseBody
    public ResponseResult add(@RequestBody Role role) {
        boolean flag = roleService.save(role);
        return ResponseResult.addResponse(flag);
    }


    @ApiOperation(value = "修改", notes = "修改角色信息")
    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody Role role) {
        boolean flag = roleService.updateById(role);
        return ResponseResult.updateResponse(flag);
    }


    @ApiOperation(value = "删除", notes = "根据id删除")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult remove(@PathVariable("id") Integer id) {
        boolean flag = roleService.removeById(id);
        return ResponseResult.removeResponse(flag);
    }


    @ApiOperation(value = "修改状态", notes = "根据id修改状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;

        boolean flag = roleService.lambdaUpdate()//使用lambda形式的修改
                .eq(Role::getId, id) //比较id
                .set(Role::getStatus, bStatus)//进行修改
                .update();//执行修改
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") int page, @RequestParam(value = "limit",required = false)Integer limit) {

        RolePageVo pageVo=roleService.findByPage(page,limit);

        return ResponseResult.queryResponse(pageVo);
    }
}

